package com.payment.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.payment.common.base.AjaxResult;
import com.payment.common.enums.CompanyId;
import com.payment.common.support.Convert;
import com.payment.common.utils.StringUtils;
import com.payment.system.util.GuangGuUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.payment.system.mapper.ApplyBankMapper;
import com.payment.system.domain.ApplyBank;
import com.payment.system.service.IApplyBankService;

/**
 * 办卡，贷款 服务层实现
 *
 * @author payment
 * @date 2019-03-18
 */
@Service
public class ApplyBankServiceImpl implements IApplyBankService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ApplyBankServiceImpl.class);

    @Autowired
    private ApplyBankMapper applyBankMapper;

    /**
     * 查询办卡，贷款信息
     *
     * @param id 办卡，贷款ID
     * @return 办卡，贷款信息
     */
    @Override
    public ApplyBank selectApplyBankById(String id) {
        return applyBankMapper.selectApplyBankById(id);
    }

    /**
     * 查询办卡，贷款列表
     *
     * @param applyBank 办卡，贷款信息
     * @return 办卡，贷款集合
     */
    @Override
    public List<ApplyBank> selectApplyBankList(ApplyBank applyBank) {
        return applyBankMapper.selectApplyBankList(applyBank);
    }

    /**
     * 新增办卡，贷款
     *
     * @param applyBank 办卡，贷款信息
     * @return 结果
     */
    @Override
    public int insertApplyBank(ApplyBank applyBank) {
        return applyBankMapper.insertApplyBank(applyBank);
    }

    /**
     * 修改办卡，贷款
     *
     * @param applyBank 办卡，贷款信息
     * @return 结果
     */
    @Override
    public int updateApplyBank(ApplyBank applyBank) {
        return applyBankMapper.updateApplyBank(applyBank);
    }

    /**
     * 删除办卡，贷款对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteApplyBankByIds(String ids) {
        return applyBankMapper.deleteApplyBankByIds(Convert.toStrArray(ids));
    }

    /**
     * 获取办卡或贷款连接地址
     *
     * @param type
     * @return
     */
    @Override
    public AjaxResult selectApplyBankByType(String type) {
        if (StringUtils.isEmpty(type)) {
            return AjaxResult.error(1, "缺失参数");
        }
        if ("10A".equals(type)) {
            try {
                JSONObject jsonObject = GuangGuUtil.getApplyBankCardUrl(CompanyId.APPLY_BANK.getCode());
                if (jsonObject == null) {
                    return AjaxResult.error(3, "获取办卡地址失败，请重试！");
                }
                String resCode = jsonObject.getString("res_code");
                String cardListUrl = jsonObject.getString("card_list_url");
                if (("YQB000000").equals(resCode)) {
                    return AjaxResult.successAndData("成功", cardListUrl);
                } else {
                    LOGGER.error("【获取办卡地址失败】{}", jsonObject.getString("res_msg"));
                    return AjaxResult.error(1, jsonObject.getString("res_msg"));
                }
            } catch (Exception e) {
                LOGGER.error("【获取办卡地址异常】{}", e);
                return AjaxResult.error(1, "获取办卡地址异常");
            }
        } else {
            return AjaxResult.error(1, "类型上传不正确");
        }


    }

}
